﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using Models;
using BLL;
using System.Text;

namespace CSMF
{
    public partial class j03 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLType();
            }
        }

        private void BindDDLType()
        {
            IList<VideoType> types = VideoTypeManager.GetAllVideoTypes();
            cbVideoType.Items.Clear();
            foreach (VideoType type in types)
            {
                ListItem li = new ListItem();
                li.Value = type.Title;
                li.Text = "&nbsp;" + type.Title + "&nbsp;";
                cbVideoType.Items.Add(li);
            }
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (this.fuInsertLogo.PostedFile.FileName != "")
            {
                lblILogo.Text = FileUploads(this, fuInsertLogo, "\\UpLoad\\email\\");
                if (!lblILogo.Text.Trim().Equals("出错"))
                {
                    lblIMessage.Text = "上传成功！";
                }
                else
                {
                    lblIMessage.Text = "上传失败！";
                }
            }
            else
            {
                lblIMessage.Text = "请选择要上传的文件！";
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            if (CheckBox1.Checked == true)
            {
                if (txtTitle.Text.Trim() != "")
                {
                    if (txtLianxiren.Text.Trim() != "")
                    {
                        if (txtLianxifangshi.Text.Trim() != "")
                        {
                            string videoType = "";
                            for (int i = 0; i < cbVideoType.Items.Count; i++)
                            {
                                if (cbVideoType.Items[i].Selected == true)
                                {
                                    if (videoType == "")
                                    {
                                        videoType = cbVideoType.Items[i].Value;
                                    }
                                    else
                                    {
                                        videoType += "," + cbVideoType.Items[i].Value;
                                    }
                                }
                            }
                            if (videoType != "")
                            {
                                string title = "剧本上传";
                                StringBuilder sb = new StringBuilder();
                                sb.Append("详细内容如下：<br><br>");
                                sb.Append("剧本名称：" + txtTitle.Text.Trim() + "<br><br>");
                                sb.Append("作品类型：" + videoType + "<br><br>");
                                sb.Append("联络人：" + txtLianxiren.Text.Trim() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;联络人联系方式:" + txtLianxifangshi.Text.Trim() + "<br><br>");
                                sb.Append("联络人联系地址：" + txtLianxidizhi.Text.Trim() + "<br><br><br><br>");
                                sb.Append("---------------------------------------------------------------------- <br><br>");
                                sb.Append("(这是一封自动产生的email，请勿回复。) <br><br>");
                                sb.Append("---------------------------------------------------------------------- <br><br>");
                                SendMail(title, sb);

                                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('信息发送成功。');", true);
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请选择作品类型。');", true);
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请填写联络人联系方式。');", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请填写联络人。');", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请填写剧本名称。');", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请先阅读“海峡两岸微电影大赛-微力十足-参赛细则”。');", true);
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cbVideoType.Items.Count; i++)
            {
                cbVideoType.Items[i].Selected = false;
            }
        }


        ///<summary>
        ///发送邮件
        ///</summary>
        public void SendMail(string title, StringBuilder sb)
        {
            string mailfrom = "join@csmff.com";
            SmtpClient smtp = new SmtpClient();
            //smtp.UseDefaultCredentials = true;
            //smtp.Credentials = new NetworkCredential("join@csmff.com", "j2o0i1n2");
            MailMessage message = new MailMessage(mailfrom, mailfrom);
            message.IsBodyHtml = true;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = title;
            if (!lblILogo.Text.Trim().Equals("出错"))
            {
                message.Attachments.Add(new Attachment(Server.MapPath("UpLoad/email/" + lblILogo.Text.Trim())));
            }
            message.Body = sb.ToString();
            smtp.Send(message);
            message.Dispose();
        }



        #region FileUp
        public static string FileUploads(Page page, FileUpload fileProduct, string url)
        {
            if (!fileProduct.HasFile)
            {
                return "出错";

            }
            try
            {
                string urlimg = DateTime.Now.ToString("hhmmss") + fileProduct.FileName;
                fileProduct.SaveAs(page.Server.MapPath(url) + urlimg);
                return urlimg;
            }
            catch
            {
                return "出错";
            }
        }
        #endregion
    }
}
